home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / ObOspec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  5.8 KB  |  238 lines  |  [TEXT/KAHL]

  1. /* ObOspec.c
  2.  * Routines to handle typeObjectSpecifier objects for Word Services
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 24 Dec 91 Mike Crawford
  14.  */
  15. #include <AppleEvents.h>
  16. #include <AEObjects.h>
  17. #include <AEPackObject.h>
  18. #include <AERegistry.h>
  19. #include "AppEvents.h"
  20. #include "ObText.h"
  21. #include "ObWind.h"
  22. #include "ObOSpec.h"
  23. #include "Gripe.h"
  24.  
  25.  
  26. /* Given the the direct object of an event is a window token (or property thereof),
  27.  * do the requested event.
  28.  */
  29. OSErr DispatchOspec( AEDesc *tokenPtr,
  30.                         AppleEvent *theAppleEventPtr,
  31.                         AppleEvent *replyEventPtr,
  32.                         long refCon )
  33. {
  34.     OSErr            err;
  35.     AEEventClass    theClass;
  36.     AEEventID        theID;
  37.     
  38.     /* This function is only for the Core suit.  Get the event ID from the appleEvent
  39.      */
  40.     
  41.     err = GetEventID( theAppleEventPtr, &theID );
  42.     
  43.     switch ( theID ){
  44.         case kAEGetData:
  45.             err = OspecGetDataHandler( tokenPtr, theAppleEventPtr, replyEventPtr, refCon );
  46.             break;
  47.         default:
  48.             err = errAEEventNotHandled;
  49.             break;
  50.     }
  51.     
  52.     return noErr;
  53. }
  54.  
  55. /* The handlers for the various events */
  56.  
  57. /* Get Data */
  58.  
  59. OSErr OspecGetDataHandler( AEDesc *tokenPtr,
  60.                         AppleEvent *theAppleEventPtr,
  61.                         AppleEvent *replyEventPtr,
  62.                         long refCon )
  63. {
  64.     DescType        propCode;
  65.     OSpecTokenBody    **tokHdl;
  66.     AEDesc            replyValue;
  67.     OSErr            err;
  68.  
  69.     /* Sanity check */
  70.     if ( tokenPtr->descriptorType != typeOSpecToken ){
  71.         Gripe( "\pGot wrong token type" );
  72.         return errAEEventNotHandled;
  73.     }
  74.  
  75.     tokHdl = (OSpecTokenBody**)(tokenPtr->dataHandle);
  76.     
  77.     propCode = (*tokHdl)->propertyCode;
  78.     
  79.     switch ( propCode ){
  80.         case typeNull:
  81.             /* This is a magic number for "Not A Property".  I don't know if this
  82.              * is really kosher - gotta ask, but it is a convenience.
  83.              */
  84.             
  85.             err = CreateWindTextSpec( (*tokHdl)->theWindowPtr,
  86.                                     (*tokHdl)->textItem,
  87.                                     &replyValue );
  88.             
  89.             if ( err ){
  90.                 Gripe( "\pCreateTextSpecifier failed" );
  91.                 return err;
  92.             }
  93.             
  94.             break;
  95.         case pName:
  96.         case pBounds:
  97.         case pClass:
  98.         case pHasTitleBar:
  99.         case pIndex:
  100.         case pIsModal:
  101.         case pIsResizable:
  102.         case pIsZoomed:
  103.         case pVisible:
  104.             Gripe( "\pGot a property type we do not yet implement" );
  105.             return errAENoSuchObject;
  106.             break;
  107.         default:
  108.             Gripe( "\pUnknown property type" );
  109.             return errAENoSuchObject;
  110.             break;
  111.     }
  112.     
  113.     /* At this point we have some kind of descriptor to stick in the reply */
  114.     
  115.     err = AEPutParamDesc( replyEventPtr,
  116.                             keyDirectObject,
  117.                             &replyValue );
  118.     if ( err ){
  119.         Gripe( "\pAEPutParamDesc failed" );
  120.         return err;
  121.     }
  122.     
  123.     err = AEDisposeDesc( &replyValue );
  124.     if ( err ){
  125.         Gripe( "\pAEDisposeDesc failed" );
  126.         return err;
  127.     }
  128.  
  129.     return noErr;
  130. }/* WindGetDataHandler */
  131.  
  132. /* Return an Ospec token given a window token */
  133.  
  134. pascal OSErr OspecFromWind(DescType desiredClass,
  135.                             const AEDesc *container,
  136.                             DescType containerClass,
  137.                             DescType form,
  138.                             const AEDesc *selectionData,
  139.                             AEDesc *theToken,
  140.                             long LongInt)
  141. {
  142.     AEDesc            longKeyData;
  143.     long            count;
  144.     OSpecTokenBody    tokData;
  145.     OSErr            err;
  146.  
  147.     /* Check that the container is what we intend.  This should only happen if we
  148.      * installed the token handler incorrectly.
  149.      */
  150.  
  151.     if ( container->descriptorType != cWindow )
  152.         return errAEEventNotHandled;
  153.     
  154.     /* find the text block based on the key form */
  155.     
  156.     switch ( form ){
  157.         case formAbsolutePosition:
  158.             /* Make sure we really have a type long descriptor */
  159.             err = AECoerceDesc( selectionData, typeLongInteger, &longKeyData );
  160.             if ( err ){
  161.                 Gripe( "\pAECoerceDesc failed" );
  162.                 return err;
  163.             }
  164.             
  165.             count = **(long**)(longKeyData.dataHandle);
  166.             
  167.             /* We're done with the descriptor created in the coercion */
  168.             
  169.             err = AEDisposeDesc( &longKeyData );
  170.             if ( err ){
  171.                 Gripe( "\pAEDisposeDesc failed" );
  172.                 return err;
  173.             }
  174.             
  175.             /* In our particular case, we can have only one text field in our table.
  176.              * A structured document would have more, so it would need a way to locate
  177.              * the item in the table right here.
  178.              *
  179.              * It is OK to return an error - it is legitimate for the speller to
  180.              * find out how many elements we have by just asking for them until we
  181.              * run out.
  182.              */
  183.  
  184. #ifndef HACK_OSPECS             
  185.             if ( count != 1L ){
  186.                 return errAENoSuchObject;
  187.             }
  188. #else
  189.             if ( count != 1L && count != 2L ){
  190.                 return errAENoSuchObject;
  191.             }
  192. #endif
  193.             
  194.             /* Actually create the token that we return */
  195.  
  196.             tokData.theWindowPtr = ((WindTokenBody*)(*(container->dataHandle)))->theWindowPtr;
  197.             tokData.textItem = count;
  198.             tokData.propertyCode = typeNull;        /* This means it's not a property */
  199.                         
  200.             err = AECreateDesc( typeOSpecToken, (Ptr)&tokData, sizeof( tokData ), theToken );
  201.             if ( err ){
  202.                 Gripe( "\pAECreateDesc failed to create a token" );
  203.                 return err;
  204.             }
  205.             
  206.             return noErr;
  207.             break;
  208.         case formRelativePosition:
  209.         case formTest:
  210.         case formRange:
  211.         case formPropertyID:
  212.         case formName:
  213.             return errAEEventNotHandled;    /* Flesh this out later */
  214.             break;
  215.         default:
  216.             Gripe( "\pGot unexpected key form" );
  217.             return errAEEventNotHandled;
  218.     }
  219.         
  220.     return noErr;
  221. }
  222.  
  223. /*
  224.  * Object counting routines.
  225.  */
  226.  
  227. OSErr CountOSpecInWind( WindowPtr wp, long *countPtr )
  228. {
  229.     /* For us, there can only be one object specifier in a window.  If you have
  230.      * a structured document such as a spreadsheet, you should count the cells that
  231.      * are selected for spellchecking.
  232.      */
  233.     
  234.     *countPtr = 1L;
  235.  
  236.     return noErr;
  237. }
  238.